package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.Team;
import com.sdry.model.llm.User;
import com.sdry.service.llm.TeamService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 班组
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:46:16
*/

@Controller
@RequestMapping("/teamMana")
public class TeamController {

	@Resource
	private TeamService teamService;  //注入班组service
	
	/**
	 * 分页查询班组信息
	 * @param page 分页条件
	 * @param team 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageTeam")
	public void listPageTeam(Page page, Team team, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (team.getTname() != null ) {
			map.put("tname", team.getTname());
		} else {
			map.put("tname", "");
			team.setTname("");
		}
		int count = teamService.countTeam(team);
		List<Team> list = teamService.listPageTeam(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存班组信息
	 * @param team 班组信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveTeam")
	public void saveTeam(Team team, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		
		//team.getTname();
		
		//Team team2 = teamService.selectByName();
		
		if (team.getTid() == null || "".equals(team.getTid())) {
			isSuccess = teamService.insertTeam(team);
		} else {
			isSuccess = teamService.updateTeam(team);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除班组
	 * @param str tid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTeam")
	public void deleteTeam(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = teamService.deleteTeam(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否可以删除班组
	 * @param str tid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteTeamOfVerify")
	public void deleteTeamOfVerify(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = teamService.deleteTeamOfVerify(str);
		ResponseUtil.write(response, isSuccess);
	}
	/**
	 * 分页查询班组的员工数据
	 * @param page
	 * @param team
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listPageUserByTeam")
	public void listPageUserByTeam(Page page, Team team, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		map.put("tid", team.getTid());
		int count = teamService.countUserByTeam(team.getTid());
		List<User> list = teamService.listPageUserByTeam(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 删除员工数据在关系表中
	 * @param emp
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteUserInEmp")
	public void deleteUserInEmp(Employee emp, HttpServletResponse response) throws Exception{
		int isSuccess = teamService.deleteUserInEmp(emp);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 查询员工是否是班组长
	 * @param emp
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/findTeamByUidInTeam")
	public void findTeamByUidInTeam(Team team, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		List<Team> list = teamService.findTeamByUidInTeam(team);
		if(list.size() > 0){
			isSuccess = 1;
		}
		ResponseUtil.write(response, isSuccess);
	}
}
